
let currentPostId
window.onload = function () {
    initRootTop()
    initRootFooter()

    initMainBar()
    initHotPost()
    initAdvertisement()



    initLogin()

    toLogin(true)

// 获取帖子id
// 获取帖子内容 评论
// 初始化帖子内容 评论

    currentPostId = window.location.href.split("?")[1].split("=")[1]
    initPostByID(currentPostId)

    initComment(currentPostId)

}

/**
 * 拆解后端返回遍历的子数据
 */
function fixComment(comment) {
    let childrenComment = []
    // let temp = 1;
    load(comment,0)

    function load(item,index) {
        item.index = index
        item.whatCommentDTOList.map(item => {
            load(item,index+1)
            item.commentUserName = "|"+item.commentUserName

            childrenComment.push(item)
        })
    }

    return childrenComment
}

function initComment(currentPostId) {
    let commentList = document.querySelector(".post_comment").firstElementChild;
    ajax("get", "/comment/getcommentbynode?node=" + currentPostId, null, function (data) {
        let codeList = data.data.map(item => {
            // console.log(item)
            commentList.innerHTML = ''
            let childrenComment = fixComment(item);
            //     加载一级评论
            let start = `
            <li class="post_comment_item">
                        <div class="post_comment_user"><span style="color:#209361;margin: 0 10px 0 0">${item.commentUserName}</span>发布于
                            <p style="color:#979797">${new Date(item.commentCreateTime)}</p></div>
                        <div class="post_comment_content">
                            <div> ${item.commentContent}</div>
                            <div class="comment_menu" onclick="show_reply_menu('reply_menu_${item.commentId}')">回复</div>
                        </div>
                        <div class="comment_reply" id="reply_menu_${item.commentId}" style="display: none">
                            <input class="reply_content" type="text" id="${item.commentId}">
                            <button class="reply_button" onclick="send_reply('reply_menu_${item.commentId}','${item.commentId}')">回复</button>
                        </div>
                        <ul class="post_comment_comment"> `


            let list = childrenComment.map(item => {
                return `
                            <li style="padding-left: ${item.index * 10}px;margin: 5px">
                                <div class="comment_comment" >
                                    <div>${item.commentUserName} <span style="color:#209361">回复</span> ${item.parentName} :${item.commentContent}</div>
                                    <div class="comment_menu" onclick="show_reply_menu('reply_menu_${item.commentId}')">回复</div>
                                </div>
                                <div class="comment_reply" id="reply_menu_${item.commentId}" style="display: none">
                                    <input class="reply_content" type="text" id="${item.commentId}">
                                    <button class="reply_button" onclick="send_reply('reply_menu_${item.commentId}','${item.commentId}')">回复</button>
                                </div>
                            </li>`
            })


            let main = ""

            for (let i = list.length - 1; i >= 0; i--) {
                main += list[i];
            }


            let end = `</ul></li>`

            return start + main + end
        })

        for (let codeListElement of codeList) {
            commentList.innerHTML += codeListElement
        }


    }, null);

}

function initPostByID(postId) {
    ajax("get", "/post/getpostbyid?postId=" + postId, null, (json) => {
        document.querySelector(".post_title").innerHTML = json.data.postTitle
        document.querySelector(".user_name").innerHTML = json.data.postUserName
        document.querySelector("#post_info").innerHTML = json.data.whatPostContent.postContent
        document.querySelector(".post_create_time").innerHTML = formatDate(new Date(json.data.postCreateTime))
        document.querySelector(".post_update_time").innerHTML = formatDate(new Date(json.data.postUpdateTime))

        //     标签
        let tag = document.querySelector(".post_tag")
        if (json.data.whatTags.length !== 0) {
            json.data.whatTags.map(({tagTitle}) => {
                tag.innerHTML += `<li>${tagTitle}</li>`
            })
        } else {
            tag.innerHTML += `<li>该帖子暂未关联标签</li>`
        }

    }, null)

}

function send_reply(menuId, inputId) {
    if (menuId != null) {
        let element = document.querySelector("#" + menuId);
        element.style.display = 'none'
    }
    let input = document.querySelector("#" + inputId)
    console.log(inputId)
    console.log(input.value)


    if (inputId === "post"){
        inputId = currentPostId
    }


    let comment = {
        commentParent:inputId,
        commentContent:input.value
    }

    console.log(comment)
    input.value = ""
    ajax("post","/comment/sendcomment",comment,function (data) {
    //     处理未登录
        if (data.code === 401){
        //     未登录
            toLogin()
        }else {
            alert(data.msg)
            window.location.reload()
        }

    })
//     刷新页面
}

function show_reply_menu(menuId) {
    let element = document.querySelector("#" + menuId);
    element.style.display = 'flex'
}